package peace.gui.controls.tooltip
{
import flash.display.Sprite;
import flash.filters.DropShadowFilter;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

import peace.data.Constraints;
/**
 * 
 * Наиболее распространенный вид тултипа
 * 
 */
public class TextTooltip extends Sprite
{
	/**
	 * максимальная ширина текста
	 */	
	protected static const MAX_WIDTH:int = 250;
	/**
	 * 
	 */	
	protected static const SHADE:DropShadowFilter = new DropShadowFilter(2, 45, 0, 0.2, 10, 10, 1, 3)
	/**
	 * контент тултипа
	 */	
	protected var label:TextField;
	/**
	 * 
	 */	
	protected var format:TextFormat;
	/**
	 * 
	 */	
	protected var padding:Constraints = new Constraints(3,3);
	/**
	 * 
	 * @param text
	 * @param format
	 * 
	 */
	public function TextTooltip(text:String = null, format:TextFormat = null)
	{
		super();
		
		this.format = format;
		
		mouseChildren = mouseEnabled = false;
		
		createContent();
		dropShadow();
		
		this.text = text;
	}
	/**
	 * 
	 * 
	 */	
	protected function createContent ():void
	{
		label = new TextField();
		label.selectable = label.mouseEnabled = false;
		
		var f:TextFormat = format || new TextFormat("Arial", 12);
		if (!format) f.align = TextFormatAlign.CENTER;
		label.defaultTextFormat = f;
		
		label.autoSize = TextFieldAutoSize.LEFT;
		
		label.x = padding.left; 
		label.y = padding.top;
		
		addChild(label);
		
	}		
	/**
	 * 
	 * 
	 */	
	protected function redrawBackground ():void
	{
		with (graphics)
		{
			clear();
			beginFill(0xffffff, 0.8);
			drawRect(0, 0, label.width + padding.left + padding.right
									, label.height + padding.top + padding.bottom);
		}
	}
	/**
	 * 
	 * 
	 */	
	protected function dropShadow ():void
	{
		filters = [SHADE];
	}
	/**
	 * 
	 * Делаем контент изменяемым в процессе работы 
	 * @param s
	 * 
	 */	
	public function set text (s:String):void
	{
		label.wordWrap = label.multiline = false;

		label.text = s;
		
		if (label.width > MAX_WIDTH)
		{
			label.wordWrap = label.multiline = true;
			label.width = MAX_WIDTH;
		}

		redrawBackground();
	}
	/**
	 * 
	 * @return 
	 * 
	 */	
	public function get text ():String
	{
		return label.text;
	}
	
}
}